import logoPng from '@/assets/logo.png';
import usePageStore from '@/store/page';
import { initModuleWH } from '@/store/page/utils';
import type { SystemWindow } from '@/types/system';
import { message } from 'ant-design-vue';
import { nanoid } from 'nanoid';
import type { ProgramEnum } from '../enum/windows.enum';
/*    */
export const windowList = ref<SystemWindow[]>([]);
export const currentWindow = ref<SystemWindow>();

/**
 * @author 吴文龙
 * @param config.component是必填项
 * @param config.title是必填项
 * @description
 * 传入一个vue组件和一个标题即可打开窗口
 *
 * 核心功能
 *
 * 在没完全了解整个AX运行逻辑前，别改动
 * 非必要，别改动
 * 能改动，别改动
 *
 *
 * 打开/新建一个窗口
 * 会自动记录用户上一次拖拽的位置/大小
 * 如果窗口已经打开，则将新值覆盖旧值
 * 新打开的窗口置顶
 * 根据屏幕尺寸，自动初始化窗口的宽高，存储在本地
 * 窗口没有设置ID，会导致无法正确删除,所以必须设定ID
 * 临时打开的窗口使用 @function getTempId 设置,在关闭浏览器后会统一销毁
 *
 */
export const openWindow = (config: SystemWindow) => {
  config.id ||= getTempId();
  config.icon ||= logoPng;

  const { width, height, x, y } = initModuleWH(config.id);

  config.x ??= x;
  config.y ??= y;
  config.w ||= width;
  config.h ||= height;
  config.hidden = false;
  config.z = windowList.value.length + 1;
  const existingWindow = windowList.value.find(e => e.id === config.id);

  if (!existingWindow) {
    windowList.value.push(config);
  } else {
    existingWindow.hidden = false;
    toTop(config.id);
    setData(config.id, config.data);
  }

  if (!config.id) {
    message.warn('窗口没有设置ID');
  }
};
/**
 * @description: 关闭窗口
 * @param {ProgramEnum} id
 */
export const closeWindow = (id: ProgramEnum | string) => {
  windowList.value = windowList.value.filter(e => {
    if (e.id !== id) {
      return e;
    }
  });
};

/**
 * @description: 窗口置顶
 * @param {ProgramEnum} id
 */
export const toTop = (id: string) => {
  windowList.value.filter(e => {
    e.z = 0;
    if (e.id === id) {
      e.z = 100;
    }
  });
};

/**
 * @description: 设置当前活动窗口
 * @param {ProgramEnum} id
 */
export const activeWindow = (id: string) => {
  windowList.value.forEach(e => {
    if (e.id === id) {
      currentWindow.value = e;
    }
  });
  toTop(id);
};
/**
 * @description:
 * 设置窗口的值,如大小,隐藏等 参照@interface SystemWindow
 * @param {string} id 窗口ID
 * @param {string} key
 * @param {any} value
 */
export const setWindowAttr = (id: string, key: keyof SystemWindow, value: any) => {
  windowList.value = windowList.value.map(e => {
    if (e.id === id) {
      e[key] = value;
    }
    return e;
  });
};

/**
 * @description: 获取窗口的值
 * @param {ProgramEnum} id
 */
export const getData = (id: string) => {
  let result: any;
  windowList.value.forEach(e => {
    if (e.id === id) {
      result = e.data;
    }
  });
  return result;
};
/**
 * @description: 设置窗口的值
 * @param {ProgramEnum} id
 * @param {any} data 任意值
 */
export const setData = (id: string, data: any) => {
  windowList.value.forEach(e => {
    if (e.id === id) {
      e.data = data;
    }
  });
};
/**
 * @description: 隐藏/显示窗口
 * @param {string} id
 * @param {boolean} flag
 */
export const toggleWindow = (id: string, flag = true) => {
  setWindowAttr(id, 'hidden', flag);
};

/* 所有窗口最小化 */
export const minWindow = () => {
  windowList.value.forEach(e => {
    if (!e.hidden) {
      e.hidden = true;
    }
  });
};
/**
 * @description: 判断窗口是否存在
 * @param {string} id
 */
export const windowIsExist = (id: string) => {
  return windowList.value.findIndex(e => e.id === id) > -1;
};

/**
 * @description:
 * 获取临时ID,方便清除
 * 这类窗口将不会记录它的大小，位置
 * ID不能用：TEMP_ID
 * @return {string}
 */
export const getTempId = (): string => {
  return `TEMP_ID:${nanoid()}`;
};

/**
 * @description:
 * 关闭页面时,销毁临时窗口
 */
export const destroyTempWindow = () => {
  const store = usePageStore();
  Object.keys(store.$state.window).forEach(e => {
    if (e.includes('TEMP_ID:')) delete store.$state.window[e];
  });
};
